let urls = require("../server/urls.js")

/************************************************
 * 接口 START 
 ************************************************/
function wxLogin(params) {
  return post(urls.Url_wx_login, params)
}
/**
 * 检测是否登录
 */
function checkLogin(code, companyId, platformId) {
  let params = {
    code: code,
    companyId: companyId, //租户ID
    platformId: platformId, //平台ID
  }
  return post(urls.Url_check_wx_login, params)
}

function ttLogin(params) {
  return post(urls.Url_tt_login, params)
}

function getConsumerById(userId) {
  return get(urls.Url_get_consumer_by_id + "/" + userId)
}
/**
 * 获等短信验证码
 */
function getVerifyCode(mobileNo) {
  return get(urls.Url_wx_get_verify_code + "/" + mobileNo, {})
}
/************************************************
 * 接口 END 
 ************************************************/

/************************************************
 * 请求封装 START 
 ************************************************/

function request(url, data = {}, contnetType = {
  'content-type': 'application/json'
}, method = 'GET') {
  return new Promise(function(success, error) {
    wx.request({
      url: url,
      data: data,
      method: method,
      header: contnetType,

      success: function(res) {
        wx.hideLoading();
        //请求成功
        if (res.statusCode == 200) {
          console.log(res.data)
          if (res.data.errno == 401) {
            loginOpertion(success, error)
          } else {
            let result = res.data
            if (result.code == 200) {
              success(result.data)
            } else {
              error(result.msg || '接口未知错误')
            }
          }
        } else {
          error("服务器异常:" + res.data.error)
        }
      },
      fail: function(err) {
        wx.hideLoading();
        error("服务器异常")
        console.log(err.errMsg)
      }
    })
  })
}

//get请求
function get(url, data = {}) {
  return request(url, data, "")
}

function get(url, data = {}, contentType) {
  return request(url, data, contentType)
}

//post请求
function post(url, data = {}) {
  return request(url, data, "", 'POST')
}

function post(url, data = {}, contentType) {
  return request(url, data, contentType, 'POST')
}

//delete请求
function _delete(url, data = {}) {
  return request(url, data, "", 'DELETE')
}

function _delete(url, data = {}, contentType) {
  return request(url, data, contentType, 'DELETE')
}

/**
 * 需要进行登录的接口---进行登录操作
 */
function loginOpertion(success, error) {
  //需要登录后才可以操作
  let code = null;
  return login().then((res) => {
    code = res.code;
    return getUserInfo();
  }).then((userInfo) => {
    //登录远程服务器
    request(api.AuthLoginByWeixin, {
      code: code,
      userInfo: userInfo
    }, 'POST').then(res => {
      if (res.error === 0) {
        //存储用户信息
        wx.setStorageSync('userInfo', res.data.userInfo);
        wx.setStorageSync('token', res.data.token);
        success(res);
      } else {
        error(res);
      }
    }).catch((err) => {
      error(err);
    });
  }).catch((err) => {
    error(err);
  })
}
/************************************************
 * 请求封装 END 
 ************************************************/

module.exports = {
  wxLogin,
  checkLogin,
  ttLogin,
  getConsumerById,
  getVerifyCode
}